package com.hfut.jpoa.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.exception.DaoException;
import com.hfut.jpoa.dao.QuoteDAO;
import com.hfut.jpoa.dto.QuoteDTO;
import com.hfut.jpoa.model.Quote;
import com.ibatis.sqlmap.client.SqlMapClient;

public class QuoteDAOImpl implements QuoteDAO
{
	private SqlMapClient sqlMapClient;

	private static final Logger LOGGER = LoggerFactory
	        .getLogger(QuoteDAOImpl.class);

	public int deleteByPrimaryKey(Integer quoteid) throws DaoException
	{
		Quote key = new Quote();
		key.setQuoteId(quoteid);
		int rows = 0;
		try
		{
			rows = sqlMapClient.delete(
			        "tb_jpoa_quote.ibatorgenerated_deleteByPrimaryKey", key);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	public void insert(Quote record) throws DaoException
	{
		try
		{
			sqlMapClient.insert("tb_jpoa_quote.ibatorgenerated_insert", record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void insertSelective(Quote record) throws DaoException
	{
		try
		{
			sqlMapClient.insert(
			        "tb_jpoa_quote.ibatorgenerated_insertSelective", record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Quote selectByPrimaryKey(Integer quoteid) throws DaoException
	{
		Quote key = new Quote();
		key.setQuoteId(quoteid);
		Quote record = null;
		try
		{
			record = (Quote) sqlMapClient.queryForObject(
			        "tb_jpoa_quote.ibatorgenerated_selectByPrimaryKey", key);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return record;
	}

	public int updateByPrimaryKeySelective(Quote record) throws DaoException
	{
		int rows = 0;
		try
		{
			rows = sqlMapClient
			        .update("tb_jpoa_quote.ibatorgenerated_updateByPrimaryKeySelective",
			                record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	public int updateByPrimaryKey(Quote record) throws DaoException
	{
		int rows = 0;
		try
		{
			rows = sqlMapClient.update(
			        "tb_jpoa_quote.ibatorgenerated_updateByPrimaryKey", record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	public SqlMapClient getSqlMapClient()
	{
		return sqlMapClient;
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient)
	{
		this.sqlMapClient = sqlMapClient;
	}

	public ResultDto<Quote> findInfoForPage(QuoteDTO condition)
	        throws DaoException
	{
		ResultDto<Quote> result = new ResultDto<Quote>();
		try
		{
			List<Quote> configsList = sqlMapClient.queryForList(
			        "tb_jpoa_quote.find_for_page", condition);
			result.setResult(configsList != null ? configsList
			        : new ArrayList<Quote>());
			BeanUtils.copyProperties(condition.getPagination(),
			        result.getPagination());
			result.getPagination().setTotal(
			        (Integer) sqlMapClient.queryForObject(
			                "tb_jpoa_quote.find_for_page_count", condition));
		} catch (SQLException e)
		{
			LOGGER.error("findInfoForPage exception", e);
			throw new DaoException("findInfoForPage exception", e);
		}
		return result;
	}

	public Quote findInfoForPage(Quote condition) throws DaoException
	{
		Quote quote = new Quote();
		try
		{
			quote = (Quote) sqlMapClient.queryForObject(
			        "tb_jpoa_quote.findInfoForPage", condition);
		} catch (SQLException e)
		{
			LOGGER.error("findInfoForPage exception", e);
			throw new DaoException("findInfoForPage exception", e);
		}
		return quote;
	}
}